﻿
namespace ECommerce
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using Data.Entity;
	using Service;

	public partial class ProductPicturesPreview : BasePage
	{
		private IProductService _ProductService;
		private IPictureService _PictureService;

		public int ProductID
		{
			get
			{
				return CommonHelper.QueryStringInt("productid");
			}
		}

		public string ProductName
		{
			get
			{
				var prodName = CommonHelper.QueryString("productname");
				if(string.IsNullOrEmpty(prodName))
				{
					if(ProductID <= 0)
					{
						return string.Empty;
					}
					else
					{
						if(_ProductService == null)
						{
							_ProductService = new ProductService();
						}

						var theProduct = _ProductService.GetProduct(ProductID);

						if(theProduct != null)
						{
							return theProduct.Name;
						}
						else
						{
							return string.Empty;
						}
					}
				}
				else
				{
					return prodName;
				}
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindPictureData();

				this.Title = ProductName;
			}
		}

		private void BindPictureData()
		{
			var data = GetPictures();

			if(data != null)
			{
				this.RepeaterPicturesNav.DataSource = data;
				this.RepeaterPicturesNav.DataBind();
			}
		}

		private List<Picture> GetPictures()
		{
			if(ProductID <= 0)
			{
				return null;
			}

			if(_PictureService == null)
			{
				_PictureService = new PictureService();
			}

			return (_PictureService.GetPicturesByProduct_WithoutBinary(ProductID)).ToList();
		}
	}
}
